Ext.define('Hi.paramadmin.view.CreateRule',{
    extend: 'Ext.form.Panel',
    layout: {
        type: 'table',
        columns: 1
    },
    frame: true,
    bodyStyle: 'padding: 10px;',
    items: [
        {
            xtype: 'hidden',
            name: 'idrule'
        },
        {
            xtype: 'hidden',
            name: 'idsystem'
        },           
        {
            xtype:'textfield',
            fieldLabel: 'Nombre Regla',
            name: 'rulename',
            allowBlank: false
            
        },
        {
            xtype: 'textarea',
            fieldLabel: 'Descripción de la Regla',
            name: 'ruledesc',
            allowBlank: false
        },
        {
            xtype: 'hidden',
            name: 'dinactive'
        }

    ],
    buttons: [
        {
            text: 'Cancelar',
            iconCls: 'btn-cancel',
            handler: function(){
                var w = this.up('window');
                w.destroy();
            }
        },       
        {
            text: 'Aceptar',
            iconCls: 'btn-save',
            handler: function(){
                var grid = Ext.getCmp('ruleGrid');
                var win = this.up('window');
                var form = this.up('form').getForm();
                if(form.isValid()){
                    win.el.mask('Guardando…', 'x-mask-loading');
                    var formData = form.getValues();
                    Ext.Ajax.request({
                      url: 'paramadmin/Index/saverule',  
                      method: 'POST',
                      params: {
                            params: Ext.encode(formData)
                      },
                    success: function(response){                            
                            var obj = Ext.decode(response.responseText);                            
                            win.el.unmask();
                            if(obj.success){
                                if(obj.action == 'create'){
                                    grid.getStore().insert(0, obj.data);
                                }else{
                                    var index = grid.getStore().find('idrule', formData['idrule']);
                                    if(index != -1){
                                        var rec = grid.getStore().getAt(index);
                                        rec.set(obj.data);
                                        rec.commit();
                                    }                                    
                                }
                                form.reset();  
                                win.close();
                            }else{
                                Ext.MessageBox.show({
                                    buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox.ERROR,
                                    msg: 'Error al Guardar la Regla',
                                    title: 'Error'
                                });
                            }                        
                        }
                    });
                }
            }
        }
       
    ]
    
});